﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;

namespace WebServiceSmart
{
    public static class ConvertTools
    {
        public static DateTime? ToDateTime(this object o)
        {
            DateTime time = DateTime.Now;
            if (o != null)
                if (DateTime.TryParse(o.ToString(), out time))
                    return time;

            return null;
        }
        public static int ToInt32(this object o, int defaultValue = 0)
        {
            if (o != null)
                int.TryParse(o.ToString(), out defaultValue);
            return defaultValue;
        }

        public static int? ToInt32Null(this object o)
        {
            int i = 0;
            if (o != null)
                if (int.TryParse(o.ToString(), out i)) return i;
            return null;
        }
        public static bool ToBool(this object o, bool defaultValue = false)
        {
            if (o != null)
                bool.TryParse(o.ToString(), out defaultValue);
            return defaultValue;
        }

        public static long ToLong(this object o, long defaultValue = 0)
        {
            if (o != null)
                long.TryParse(o.ToString(), out defaultValue);
            return defaultValue;
        }

        public static double ToDouble(this object o, double defaultValue = 0)
        {
            if (o != null)
                double.TryParse(o.ToString(), out defaultValue);
            return defaultValue;
        }


        public static List<Dictionary<string, string>> ToModels(this DataTable dt)
        {
            var list = new List<Dictionary<string, string>>();
            if (dt != null && dt.Rows.Count > 0)
            {
                foreach (DataRow item in dt.Rows)
                {
                    var model = new Dictionary<string, string>();
                    foreach (DataColumn cItem in dt.Columns)
                    {
                        if (!model.ContainsKey(cItem.ColumnName))
                        {
                            model.Add(cItem.ColumnName, item[cItem.ColumnName]?.ToString() ?? string.Empty);
                        }
                    }
                    list.Add(model);
                }
            }
            return list;
        }


        public static IEnumerable<T> Remove<T>(this IEnumerable<T> lis, Func<T, bool> predicate)
        {

            return lis;
        }
        public static int IndexOf<T>(this IEnumerable<T> lis, Func<T, bool> predicate)
        {
            var index = 0;
            foreach (var item in lis)
            {
                if (predicate(item)) return index;
                index++;
            }
            return 0;
        }

        public static bool Contains<T>(this IEnumerable<T> lis, Func<T, bool> predicate)
        {
            var index = 0;
            foreach (var item in lis)
            {
                if (predicate(item)) return true;
                index++;
            }
            return false;
        }
    }
}